<template>
  <div>
    <el-dialog title="新增员工" :visible="showDialog" @close="onCancelBtn">
      <el-form ref="refForm" :model="newEmployeeData" :rules="rules">
        <el-form-item label="姓名" label-width="120px" prop="username">
          <el-input v-model.trim="newEmployeeData.username" autocomplete="off" placeholder="请输入姓名" style="width:50%" />
        </el-form-item>
        <el-form-item label="手机" label-width="120px" prop="mobile">
          <el-input v-model.trim="newEmployeeData.mobile" autocomplete="off" placeholder="请输入手机号" style="width:50%" />
        </el-form-item>
        <el-form-item label="入职日期" label-width="120px" prop="timeOfEntry">
          <el-date-picker
            v-model="newEmployeeData.timeOfEntry"
            type="date"
            placeholder="请选择日期"
            style="width:50%"
          />
        </el-form-item>
        <el-form-item label="聘用形式" label-width="120px" prop="formOfEmployment">
          <el-select v-model="newEmployeeData.formOfEmployment" placeholder="请选择聘用形式" style="width:50%">
            <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" label-width="120px" prop="workNumber">
          <el-input v-model.trim="newEmployeeData.workNumber" autocomplete="off" placeholder="请输入工号" style="width:50%" />
        </el-form-item>
        <el-form-item label="部门" label-width="120px" prop="departmentName">
          <el-input v-model="newEmployeeData.departmentName" autocomplete="off" placeholder="请选择部门" style="width:50%" @focus="showTree=true" />
          <el-tree v-if="showTree" v-loading="loading" :data="treeData" :props="defaultProps" default-expand-all @node-click="selectNode" />
        </el-form-item>
        <el-form-item label="转正日期" label-width="120px" prop="correctionTime">
          <el-date-picker
            v-model="newEmployeeData.correctionTime"
            type="date"
            placeholder="请选择转正日期"
            style="width:50%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onCancelBtn">取 消</el-button>
        <el-button type="primary" @click="onSureBtn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments } from '@/api/departments.js';
// 导入一个将平级数组转换成树形数据的方法
import { transListToTreeData } from '@/utils/index.js';
// 导入聘用形式的静态资源数据
import EmployeeEnum from '@/api/constant/employees.js';
// 导入新增部门员工的方法
import { addNewEmployee } from '@/api/employee.js';
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 定义静态资源数据
      EmployeeEnum,
      // 存储树形结构的数据
      treeData: [],
      // 处理树形控件的进度条
      loading: false,
      // 树形控件显示的数据
      defaultProps: {
        label: 'name',
        children: 'children',
      },
      showTree: false,
      // 新增员工的表单数据
      newEmployeeData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: '',
      },
      // 表单校验规则
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur',
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur',
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.loadDepartments();
  },
  methods: {
    // 获取部门信息
    async loadDepartments() {
      const { depts } = await getDepartments();
      this.loading = true;
      this.treeData = transListToTreeData(depts, '');
      this.loading = false;
      // console.log(this.treeData);
    },
    selectNode(node) {
      // console.log(arguments);
      // console.log(node.name);
      this.newEmployeeData.departmentName = node.name;
      this.showTree = false;
    },
    // 点击确定校验表单
    async onSureBtn() {
      try {
        // 手动校验表单
        await this.$refs.refForm.validate();
        await addNewEmployee(this.newEmployeeData);
        this.$emit('updateEmployee');
        this.$emit('update:showDialog', false);
        this.$message.success('新增员工成功！');
      } catch (error) {
        console.log(error);
      }
    },
    // 点击取消
    onCancelBtn() {
      this.newEmployeeData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: '',
      };
      // 移除校验规则
      this.$refs.refForm.resetFields();
      this.$emit('update:showDialog', false);
    },
  },
};

</script>

<style>

</style>
